function mouseX(evt) { if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX) return evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft); else return 0; }
function mouseY(evt) { if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY) return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0; }
function pageWidth() { return window.innerWidth != null ? window.innerWidth : document.documentElement != null ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null; }
function pageHeight() { return window.innerHeight != null ? window.innerHeight : document.documentElement != null ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null; }
function posLeft() { return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0; }
function posTop() { return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0; }
function posRight() { return posLeft() + pageWidth(); }
function posBottom() { return posTop() + pageHeight(); }

var cX = 0; var cY = 0;
var tooltipObj = '';
function UpdateCursorPosition(evt)
{
	cX = mouseX(evt);
	cY = mouseY(evt);
	if (tooltipObj != '')
		align_tip(tooltipObj);
}
document.onmousemove = UpdateCursorPosition;

function align_tip(tooltipId)
{
	var it = document.getElementById(tooltipId);
	if (!it)
	{
		//alert('not found:'+tooltipId);
		return;
	}

	var tl_ArrowNode = document.getElementById('tla_' + tooltipId);
	var bl_ArrowNode = document.getElementById('bla_' + tooltipId);
	var tr_ArrowNode = document.getElementById('tra_' + tooltipId);
	var br_ArrowNode = document.getElementById('bra_' + tooltipId);

	var x = cX + 3;
	var y = cY + 2;

	if (cY + it.offsetHeight > posBottom() - 20)
	{
		y = cY - it.offsetHeight - 20;
		if (cX + it.offsetWidth > posRight() - 40)
		{
			x = cX - it.offsetWidth - 30;
			if (tl_ArrowNode != null)
				tl_ArrowNode.style.display = "none";
			if (tr_ArrowNode != null)
				tr_ArrowNode.style.display = "none";
			if (bl_ArrowNode != null)
				bl_ArrowNode.style.display = "none";
			if (br_ArrowNode != null)
				br_ArrowNode.style.display = "inline";
		}
		else
		{
			if (tl_ArrowNode != null)
				tl_ArrowNode.style.display = "none";
			if (tr_ArrowNode != null)
				tr_ArrowNode.style.display = "none";
			if (bl_ArrowNode != null)
				bl_ArrowNode.style.display = "inline";
			if (br_ArrowNode != null)
				br_ArrowNode.style.display = "none";
		}
	}
	else
	{
		if (cX + it.offsetWidth > posRight() - 40)
		{
			x = cX - it.offsetWidth - 2;
			if (tl_ArrowNode != null)
				tl_ArrowNode.style.display = "none";
			if (tr_ArrowNode != null)
				tr_ArrowNode.style.display = "inline";
			if (bl_ArrowNode != null)
				bl_ArrowNode.style.display = "none";
			if (br_ArrowNode != null)
				br_ArrowNode.style.display = "none";
		}
		else
		{
			if (tl_ArrowNode != null)
				tl_ArrowNode.style.display = "inline";
			if (tr_ArrowNode != null)
				tr_ArrowNode.style.display = "none";
			if (bl_ArrowNode != null)
				bl_ArrowNode.style.display = "none";
			if (br_ArrowNode != null)
				br_ArrowNode.style.display = "none";
		}
	}
	it.style.top = y + 'px';
	it.style.left = x + 'px';
}


function show_tip(tooltipId)
{
	var it = document.getElementById(tooltipId);
	if (!it)
	{
		//alert('not found:'+tooltipId);
		return;
	}
	tooltipObj = tooltipId;
	align_tip(tooltipId);
	it.style.visibility = 'visible';
}

function hide_tip(tooltipId)
{
	var it = document.getElementById(tooltipId);
	if (!it) return;
	tooltipObj = '';
	it.style.visibility = 'hidden';
}

